package com.ysxsoft.wdg.ui.activity.my.partner;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.JfOrderAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.FenOrderResponse;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class FsOrderActivity extends BaseActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, FsOrderActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_one_num)
    TextView tvOneNum;
    @BindView(R.id.tv_two_num)
    TextView tvTwoNum;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private JfOrderAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fs_order;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this, true);
        initStatusBar(topView, true);
        titleContent.setText("粉丝订单");
        tvOneNum.setSelected(true);
        adapter = new JfOrderAdapter(R.layout.item_fs_order);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setAdapter(adapter);

        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getList();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
    }

    @OnClick({R.id.title_finish, R.id.tv_one_num, R.id.tv_two_num})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_one_num:
                tvOneNum.setSelected(true);
                tvTwoNum.setSelected(false);
                page = 1;
                type=1;
                getList();
                break;
            case R.id.tv_two_num:
                tvOneNum.setSelected(false);
                tvTwoNum.setSelected(true);
                page = 1;
                type=2;
                getList();
                break;
        }
    }

    private int page = 1;
    private int type = 1;

    /**
     * 获取精选
     */
    private void getList() {
        OkHttpUtils.post()
                .url(UrlHost.FEN_ORDER_LIST)
//                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .addParams("type", type + "")//1.粉丝销量   2.即将到账
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    FenOrderResponse fenOrderResponse = JsonUtils.parseByGson(response, FenOrderResponse.class);
                    if (fenOrderResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(fenOrderResponse.getCode())) {
                        FenOrderResponse.DataBean data = fenOrderResponse.getData();
                        tvOneNum.setText("粉丝销量("+data.getCount()+")");
                        tvTwoNum.setText("即将到账("+data.getYu()+")");
                        if (data != null) {
                            List<FenOrderResponse.DataBean.ListBean> list = data.getList();
                            if (page == 1) {
                                multStatusView.showContent();
                                if (list != null && !list.isEmpty()) {
                                } else {
                                    multStatusView.showEmpty();
                                }
                                adapter.setNewData(list);
                            } else {
                                adapter.addData(list);
                            }
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }
}
